package org.lzfto.flink.demo.system.config;

import java.io.IOException;

import org.lzfto.flink.demo.common.data.PageData;
import org.lzfto.flink.demo.common.result.PubResult;
import org.lzfto.flink.demo.config.AppInfo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/sys-config")
public class SysConfigController {
    private final SysConfigService sysConfigService;

    public SysConfigController(SysConfigService sysConfigService) {
        this.sysConfigService = sysConfigService;
    }

    @GetMapping("/getByKey/{configKey}")
    public PubResult<String> getConfigValue(@PathVariable String configKey) {
        return PubResult.ok(sysConfigService.getConfigValue(configKey));
    }

    @GetMapping("/getById/{id}")
    public PubResult<SysConfig> getById(@PathVariable int id) {
        return PubResult.ok_data(sysConfigService.selectById(id));
    }

    @GetMapping("/getAll")
    public PubResult<PageData<SysConfig>> getAll(int pageNo,int pageSize,SysConfig sysConfig) {
        return PubResult.ok_data(sysConfigService.selectAll(pageNo,pageSize,sysConfig));
    }

    @PostMapping("/add")
    public PubResult<SysConfig> create(@RequestBody SysConfig sysConfig) {
        sysConfigService.insert(sysConfig);
        return PubResult.ok("成功",null);
    }

    @PostMapping("/update/{id}")
    public PubResult<Void> update(@PathVariable int id, @RequestBody SysConfig sysConfig) {
        sysConfig.setId(id);
        sysConfigService.update(sysConfig);
        return PubResult.ok("成功");
    }

    @PostMapping("/delete/{id}")
    public PubResult<Void> delete(@PathVariable int id) {
        sysConfigService.deleteById(id);
        return PubResult.ok("成功");
    }

    @GetMapping("/getAccount")
    public PubResult<Account> getAccount(String password) {
        try {
            return PubResult.ok_data(sysConfigService.getAccount(password));
        } catch (IOException e) {
            e.printStackTrace();
            return PubResult.error("失败");
        }
    }
    
    @GetMapping("/getAppInfo")
    public PubResult<AppInfo> getAppInfo() {
    	return PubResult.ok_data(sysConfigService.getAppInfo());
    }
    
}
